//
//  YbxqVC.swift
//  Auscultation
//
//  Created by 来 on 2020/12/9.
//

import UIKit

class YbxqVC: BaseVC {
    
    var type: String = ""
    
    var tzzx: UILabel!
    var collectionView: UICollectionView!
    var dataArray: [[String: Any]] = []
    var pageNum: Int = 1
    var pageSize: Int = 10
    var keyWord: String = ""
    
    var selectYb: [Int] = []

    override func viewDidLoad() {
        super.viewDidLoad()

        initUI()
        collectionView.mj_header = MJRefreshNormalHeader.init(refreshingTarget: self, refreshingAction: #selector(loadData))
        collectionView.mj_footer = MJRefreshAutoNormalFooter.init(refreshingTarget: self, refreshingAction: #selector(loadMoreData))
        loadData()
    }
    
    // 数据刷新
    @objc func loadData() {
        pageNum = 1
        dataArray.removeAll()
        loadMoreData()
    }
    @objc func loadMoreData() {
        var paramet: [String: Any] = [:]
        var method = ""
        if type == "标注样本" {
            paramet = [
                "keyWord": keyWord,
                "pageNo": pageNum.description,
                "pageSize": pageSize.description,
                "labelStatus": 0,
                "hospitalId": EWEUser.lgf.hospitalId ?? "",
                "status": 20
            ]
            method = "sample"
        } else if type == "审核样本" {
            paramet = [
                "keyWord": keyWord,
                "pageNo": pageNum.description,
                "pageSize": pageSize.description,
                "roleId": "57782488939888640",
                "isDeleted": 1,
                "status": 20,
                "auditStatus": 0,
            ]
            method = "difficult"
        }
        EWERequest.lgf(method, paramet, .get) { [weak self] (data) in
            let result = (data["result"] as? [[String: Any]]) ?? []
            if result.count > 0 {
                self?.dataArray += result
                self?.pageNum += 1
            }
            self?.collectionView.reloadData()
            self?.endRefreshing()
            if result.count == 0 {
                self?.collectionView.mj_footer?.endRefreshingWithNoMoreData()
            }
        } _: { [weak self] (error) in
            SVProgressHUD.showError(error)
            self?.endRefreshing()
        }
    }
    func endRefreshing() {
        self.collectionView.mj_header?.endRefreshing()
        self.collectionView.mj_footer?.endRefreshing()
    }
    
    // 下一步
    override func rightBtnSelect(_ sender: UIButton) {
        var sample: [[String: String]] = []
        selectYb.forEach { (index) in
            if type == "标注样本" {
                sample.append(["id": (dataArray[index]["id"] as? String) ?? ""])
            } else {
                sample.append(["id": (dataArray[index]["sampleId"] as? String) ?? ""])
            }
        }
        if sample.count == 0 {
            SVProgressHUD.showError("请选择样本")
            return
        }
        let vc = RylbVC()
        vc.sample = sample
        vc.type = type
        self.navigationController?.show(vc, sender: self)
    }

}

extension YbxqVC: UITextFieldDelegate {
    func textFieldDidEndEditing(_ textField: UITextField) {
        keyWord = textField.text ?? ""
        loadData()
    }
}

extension YbxqVC : UICollectionViewDelegateFlowLayout, UICollectionViewDelegate, UICollectionViewDataSource {
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize  {
        return CGSize.init(width: UIDevice.lgf_ScreenW / 2, height: 110)
    }
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return dataArray.count
    }
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.lgf_DequeueReusableCell(with: YbxqCell.self, for: indexPath)
        cell.tzzx.text = (dataArray[indexPath.item]["no"] as? String) ?? ""
        cell.tw.text = "" + ((dataArray[indexPath.item]["position"] as? String) ?? "")
        cell.phcd.text = "" + ((dataArray[indexPath.item]["coordinate"] as? String) ?? "")
        cell.indexPath(indexPath)
        cell.back_view.layer.borderWidth = 3.0
        if selectYb.contains(indexPath.item) {
            cell.back_view.layer.borderColor = UIColor.init(lgf_HexString: "0F88F2")?.withAlphaComponent(0.7).cgColor
        } else {
            cell.back_view.layer.borderColor = UIColor.white.cgColor
        }
        return cell
    }
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        if selectYb.contains(indexPath.item) {
            selectYb.lgf_Remove(indexPath.item)
        } else {
            selectYb.append(indexPath.item)
        }
        collectionView.reloadData()
    }
}
